#include "head.h"

/**
 * 参数：所有用户的信息
 * 返回值：登录成功后返回用户信息
 */
U_data Log_in(U_list *list)
{
    if (list == NULL)
    {
        printf("S_List_log_in : list = NULL\n");
        exit(-1);
    }
    if (list->next == NULL)
    {
        printf("S_List_log_in : list->next = NULL\n");
        exit(-1);
    }

    U_data data;

    // 开始登录
    while (1)
    {
        system("clear");
        printf("请输入账号: ");
        scanf("%s", data.name);

        printf("请输入密码: ");
        scanf("%s", data.passwd);

        U_node *ptr = list->next;

        while (ptr != NULL)
        {
            // 比较用户名
            if (strcmp(data.name, ptr->data.name) == 0)
            {
                // 比较密码
                if (strcmp(data.passwd, ptr->data.passwd) == 0)
                {
                    return ptr->data; // 登录成功，返回用户信息
                }
            }
            ptr = ptr->next;
        }
        printf("用户名或密码错误，请重新输入。\n");
        sleep(2);
    }
}
// 注册
void reg(FlightSystem * Flight_sys , U_list *list)
{
    U_data log_data;
    char password[128];
    while(1)
    {
        while (1)
        {
            printf("请输入你 账号:\n");
            scanf("%s" , log_data.name);
            int K = S_List_find_name_n(list, log_data.name);
            if (K == 1)
            {
                printf("当前用户名已被注册\n");
            }
            else break;
        }
        
        printf("请输入你的密码:\n");
        scanf("%s" , log_data.passwd);

        printf("请再次输入你的密码:\n");
        scanf("%s" , password);
        
        if (strcmp(password , log_data.passwd))
        {
            printf("两次密码输入不相同 请重新输入\n");
        }
        else break;
    }
    int flag = 0;int root;
    const wchar_t *title_grade = L"请选择您的用户等级(折扣)(充值金额)";
    const wchar_t *option_grade[] = {
    L"普通用户",
    L"白银会员(9折)(10000)",
    L"黄金会员(8折)(15000)",
    L"白金会员(7折)(20000)",
    L"管理员(需要密码验证)",
    L"返回"};
    flag = display_menu(title_grade, option_grade, 6);
    switch (flag) 
    {  
        case 1:  
            log_data.numTickets=0;
            log_data.value=0;
            log_data.grade = 2;
            log_data.accumulated = 0;
            S_List_insert(Flight_sys->REGULAR_USER , log_data , 1);
            S_List_File_print( Flight_sys->REGULAR_USER , REGULAR_FILE);
            printf("恭喜您注册普通用户成功,以下是您的用户信息:\n");
            S_List_print(Flight_sys->REGULAR_USER);
            sleep(2); 
            return ;
        case 2:  
            log_data.numTickets=0;
            log_data.value=10000;
            log_data.grade = 3;
            log_data.accumulated = 10000;
            S_List_insert(Flight_sys->SILVER_USER , log_data , 1);
            S_List_File_print( Flight_sys->SILVER_USER , SILVER_FILE );
            printf("恭喜您注册白银会员成功,以下是您的用户信息:\n");
            S_List_print(Flight_sys->SILVER_USER);
            sleep(2); 
            return ;  
        case 3:  
            log_data.numTickets=0;
            log_data.value=15000;
            log_data.grade = 4;
            log_data.accumulated = 15000;
            S_List_insert(Flight_sys->GOLD_USER , log_data , 1);
            S_List_File_print( Flight_sys->GOLD_USER , GOLD_FILE );
            printf("恭喜您注册黄金会员成功,以下是您的用户信息:\n");
            S_List_print(Flight_sys->GOLD_USER);
            sleep(2);   
            return ;
        case 4:  
            log_data.numTickets=0;
            log_data.value=20000;
            log_data.grade = 5;
            log_data.accumulated = 20000;
            S_List_insert(Flight_sys->PLATINUM_USER , log_data , 1);
            S_List_File_print( Flight_sys->PLATINUM_USER , PLATINUM_FILE );
            printf("恭喜您注册白金会员成功,以下是您的用户信息:\n");
            S_List_print(Flight_sys->PLATINUM_USER);
            sleep(2);   
            return ;
        case 5:  
            printf("请输入系统管理员验证码: ");
            while(1)
            {
                scanf("%d", &root);
                if(root == Root_password)
                {
                    log_data.numTickets=0;
                    log_data.value=1000;
                    log_data.grade = 1;
                    log_data.accumulated = 1000;
                    S_List_insert(Flight_sys->ADMIN_USER , log_data , 1);
                    S_List_File_print( Flight_sys->ADMIN_USER , ADMIN_FILE );
                    printf("恭喜您注册管理员用户成功,以下是您的用户信息:\n");
                    S_List_print(Flight_sys->ADMIN_USER);
                    sleep(2); 
                    return ;
                }
                else printf("验证码错误,请重新输入\n");
            }
            break;
        case 6:
            return ;
        default:
            break;
    }  
}
